export DEBUG = y
export LOG_DEBUG = y

build_flags = -Wall -Wconversion -Wextra

export GIT_VERSION := $(shell git rev-parse --symbolic-full-name --abbrev-ref HEAD)

ccc_flags = -Wall -c -m64 -DST_PIO -DLINUX -fPIC -DMEBSVERSION=\"$(GIT_VERSION)\" \
			-DZERO_FILL_CRC -DCLIENT_CHECKSUM -DDIV_CHUNK_SIZE=4194304 \
			-DALLOC_CHECKSUM -DCONCURRENT_TH_NB=50 -DUSE_WRITE_WITH_VERSION \
			-Wno-pointer-arith -Werror=type-limits -Wsign-compare
#			-Wno-unused-result

ifneq ($(DEBUG), y)
	ccc_flags += -DMAX_FD
endif

ifeq ($(build), y)
	ccc_flags += $(build_flags)
endif

ifneq ($(X86),y)
	ccc_flags += -march=native
else
	ccc_flags += -march=x86-64
endif

ifneq ($(DEBUG),y)
	 ccc_flags += -O3 -g
else
	 ccc_flags += -g3
endif

ifeq ($(DEBUG_GROUP),y)
	ccc_flags += -DDEBUG_WRITE_GROUP
endif

ifeq ($(LOG_DEBUG),y)
	ccc_flags += -DINIT_LOG_LEVEL=0
endif

export cc_flags = -std=gnu99 $(ccc_flags)
export cpp_flags = $(ccc_flags) -D__STDC_LIMIT_MACROS \
                   -D__STDC_CONSTANT_MACROS -Wno-invalid-offsetof
#                   -Wno-literal-suffix

export cc_include_dirs = ../contrib/stat-threads/ ../contrib/hiredis/
cc_include = $(addprefix -I, $(cc_include_dirs))
export cpp_include_dirs =
cpp_include = $(addprefix -I, $(cpp_include_dirs)) $(cc_include)

export g++ = g++
cc = cc $(cc_flags) $(cc_include)
cpp = $(g++) $(cpp_flags) $(cpp_include)

ssl_ld_flags = -lcrypto
ldflags = $(ssl_ld_flags) -rdynamic -laio -lm -msse4.2 -lstdc++ -lrt -ldl -lpthread -msse4.2
ld = gcc -Wall $(ldflags)
ifneq ($(DEBUG),y)
	 ld += -g
else
	 ld += -g3
endif

mkdir = mkdir -p
rm = rm -rf


export common-static-libs = ../contrib/hiredis/libhiredis.a
#                            ../contrib/zlib/libz.a ../contrib/libjson-c/libjson-c.a

interfaces := ioengine.o buffer.o

lib-objs1 := incubator.o ringbuffer.o debug.o log.o netserver.o heartbeat.o networking.o \
             redis-client.o \
             backend-fs.o journal-lite.o range-lock.o chunk-client.o \
             utcache.o identity-pool.o redis-single.o st-sync-redis.o \
             connection-pool.o concurrent-connection-pool.o crc32.o \
             master-request.o mmap-manager.o thread-pool.o \
             $(common-static-libs) $(interfaces)

chunkserver-journal-objs = chunkserver-journal.o \
                           chunkserver-journal-entry.o cache-entry.o

chunkserver-common-objs = ../contrib/stat-threads/libst.a \
                          endpoints.o f-ops.o st_pio.o statistics.o operations.o \
                          $(chunkserver-journal-objs)

chunkserver-objs = chunkserver.o \
                   $(lib-objs1) $(chunkserver-common-objs)
export OBJDIR := ../objs
chunkserver-objs-zym := $(patsubst %.o, $(OBJDIR)/%.o, $(chunkserver-objs))

exes = chunk-server
exes_dir = ../sandbox/bin


all: $(OBJDIR) $(exes) $(exes)
	@if [ ! -d "$(exes_dir)" ]; then \
		$(mkdir) $(exes_dir);\
	fi
	@for i in $(exes); do \
		cp $$i $(exes_dir); \
	done
	@cp -r conf/ ${exes_dir};
	@cp -r logs/ ${exes_dir};

$(OBJDIR):
	[ -d $@ ] || \
	$(mkdir) $(OBJDIR) ; \
	for i in $$(cd $(CURDIR) ; find . -type d) ; do \
                $(mkdir) $@/$$i ; \
	done

chunk-server: $(chunkserver-objs-zym)
	@echo +++++++++++++++++chunkserver-objs-zym: $(chunkserver-objs-zym)
	$(ld) -o $@ $(chunkserver-objs-zym)

$(OBJDIR)/chunkserver.o: chunkserver.cpp ursax.h debug.h log.h heartbeat.h
	$(cpp) chunkserver.cpp -o $@
	$(cpp) -E chunkserver.cpp -o $(OBJDIR)/chunkserver.zym

$(OBJDIR)/buffer.o: buffer.cpp buffer.h
	$(cpp) buffer.cpp -o $@

$(OBJDIR)/ringbuffer.o: ringbuffer.cpp ringbuffer.h
	$(cpp) ringbuffer.cpp -o $@

$(OBJDIR)/debug.o: debug.cpp debug.h
	$(cpp) debug.cpp -o $@

$(OBJDIR)/log.o: log.cpp log.h
	$(cpp) log.cpp -o $@

$(OBJDIR)/netserver.o: netserver.cpp netserver.h
	$(cpp) netserver.cpp -o $@

$(OBJDIR)/heartbeat.o: heartbeat.cpp heartbeat.h st-redis.h
	$(cpp) heartbeat.cpp -o $@

$(OBJDIR)/endpoints.o: endpoints.cpp endpoints.h
	$(cpp) endpoints.cpp -o $@

$(OBJDIR)/networking.o: networking.cpp networking.h
	$(cpp) networking.cpp -o $@

$(OBJDIR)/redis-client.o: redis-client.cpp redis-client.h
	$(cpp) redis-client.cpp -o $@

$(OBJDIR)/backend-fs.o: backend-fs.cpp backend-fs.h
	$(cpp) backend-fs.cpp -o $@

$(OBJDIR)/f-ops.o: f-ops.cpp f-ops.h
	$(cpp) f-ops.cpp -o $@

$(OBJDIR)/ioengine.o: ioengine.cpp ioengine.h
	$(cpp) ioengine.cpp -o $@

$(OBJDIR)/st_pio.o: st_pio.cpp st_pio.h
	$(cpp) st_pio.cpp -o $@

$(OBJDIR)/journal-lite.o: journal-lite.cpp journal-lite.h log.h
	$(cpp) journal-lite.cpp -o $@

$(OBJDIR)/range-lock.o: range-lock.cpp range-lock.h ursax.h
	$(cpp) range-lock.cpp -o $@

$(OBJDIR)/chunk-client.o: chunk-client.cpp chunk-client.h log.h protocol.h ursax.h
	$(cpp) chunk-client.cpp -o $@

$(OBJDIR)/utcache.o: utcache.c utcache.h uthash.h
	$(cc) utcache.c -o $@

$(OBJDIR)/identity-pool.o: identity-pool.cpp identity-pool.h
	$(cpp) identity-pool.cpp  -o $@

$(OBJDIR)/redis-single.o: redis-single.cpp redis-single.h
	$(cpp) redis-single.cpp -o $@

$(OBJDIR)/st-sync-redis.o: st-sync-redis.cpp st-redis.h log.h
	$(cpp) st-sync-redis.cpp -o $@

$(OBJDIR)/connection-pool.o: connection-pool.cpp log.h utcache.h connection-pool.h
	$(cpp) connection-pool.cpp -o $@

$(OBJDIR)/concurrent-connection-pool.o: concurrent-connection-pool.cpp \
                                        concurrent-connection-pool.h log.h utcache.h \
                                        pooled-functions.h
	$(cpp) concurrent-connection-pool.cpp -o $@

$(OBJDIR)/crc32.o: crc32.cpp crc32.h
	$(cpp) -msse4.2 crc32.cpp -o $@

$(OBJDIR)/master-request.o: master-request.cpp master-request.h
	$(cpp) master-request.cpp -o $@

$(OBJDIR)/statistics.o: statistics.cpp statistics.h backend-fs.h connection-pool.h netserver.h
	$(cpp) statistics.cpp -lm -o $@

$(OBJDIR)/mmap-manager.o: mmap-manager.cpp
	$(cpp) mmap-manager.cpp -o $@

$(OBJDIR)/incubator.o: incubator.cpp incubator.h backend-fs.h protocol.h range-lock.h \
                       utcache.h log.h
	$(cpp) incubator.cpp -o $@

$(OBJDIR)/operations.o: operations.cpp operations.h log.h protocol.h backend-fs.h \
                        incubator.h identity-pool.h utcache.h identity-pool.h \
                        master-request.h ringbuffer.h
	$(cpp) operations.cpp -o $@

$(OBJDIR)/thread-pool.o: thread-pool.cpp thread-pool.h identity-pool.h
	$(cpp) thread-pool.cpp -o $@

$(OBJDIR)/chunkserver-journal.o: chunkserver-journal/chunkserver-journal.cpp chunkserver-journal/chunkserver-journal.h
	$(cpp) chunkserver-journal/chunkserver-journal.cpp -o $@

$(OBJDIR)/chunkserver-journal-entry.o: chunkserver-journal/chunkserver-journal-entry.cpp chunkserver-journal/chunkserver-journal-entry.h
	$(cpp) chunkserver-journal/chunkserver-journal-entry.cpp -o $@

$(OBJDIR)/cache-entry.o: ./module-client/disk-cache/cache-entry.cpp ./module-client/disk-cache/cache-entry.h
	$(cpp) ./module-client/disk-cache/cache-entry.cpp -o $@




clean:
	$(rm) *.o *.zym $(exes)
	$(rm) $(OBJDIR) $(exes_dir)

.PHONY: print test

print:
	@echo "=============== cc ===============: " $(cc)
	@echo "============== cpp ===============: " $(cpp)
	@echo "=============== ld ===============: " $(ld)
	@echo "============= ccc_flags ==========: " $(ccc_flags)

test:
	@echo +++++++++++++++++++++++++++++++ $(OBJDIR)















